package cn.t.util.casestudy.encode;

import cn.t.util.common.digital.HexUtil;
import org.msgpack.MessagePack;
import org.msgpack.template.Templates;
import org.msgpack.type.Value;
import org.msgpack.unpacker.Converter;

import java.util.ArrayList;
import java.util.List;

public class MessagePackUsage {
    public static void main(String[] args) throws Exception {
        List<String> src = new ArrayList<>();
        src.add("msgpack");
        src.add("kumofs");
        src.add("viver");

        MessagePack msgpack = new MessagePack();
// Serialize
        byte[] raw = msgpack.write(src);

// Deserialize directly using a template
        List<String> dst1 = msgpack.read(raw, Templates.tList(Templates.TString));
        System.out.println(dst1.get(0));
        System.out.println(dst1.get(1));
        System.out.println(dst1.get(2));

// Or, Deserialze to Value then convert type.
        Value dynamic = msgpack.read(raw);
        List<String> dst2 = new Converter(dynamic)
            .read(Templates.tList(Templates.TString));
        System.out.println(dst2.get(0));
        System.out.println(dst2.get(1));
        System.out.println(dst2.get(2));


//        String hexStr = "8000c2120003000163020100016103000d0001701200030001630800096d61696c2e73656e6400017204ffffffff000170120007000b636d644261736554696d6508000a31353634363334363931000a616c6c69616e63654964080000000974617267657455696408000f33343835303733323130303035333700057469746c650800000008636f6e74656e74730800053131313131000d73656e644c6f63616c54696d6508000a3135363436333436393000046e616d6508000ae596b55e5f5ee596b532";
        String hexStr = "8000c2120003000163020100016103000d0001701200030001630800096d61696c2e73656e6400017204ffffffff000170120007000b636d644261736554696d6508000a31353634363334363933000a616c6c69616e63654964080000000974617267657455696408000f33343835303733323130303035333700057469746c650800000008636f6e74656e74730800053131313131000d73656e644c6f63616c54696d6508000a3135363436333436393200046e616d6508000ae596b55e5f5ee596b532";
        byte[] hexBytes = HexUtil.stringToBytes(hexStr);
        System.out.println(msgpack.read(hexBytes));
    }
}
